package repository

import (
	"database/sql"
	"fmt"
	"strings"
	"time"

	"go-file-perception-model/internal/model"

	_ "modernc.org/sqlite"
)

// FileMonitorRepository 文件监控仓库接口
type FileMonitorRepository interface {
	// 目录监控相关
	CreateDirectoryRecord(record *model.DirectoryMonitorRecord) error
	GetDirectoryRecordByID(id string) (*model.DirectoryMonitorRecord, error)
	GetDirectoryRecordByPath(path string) (*model.DirectoryMonitorRecord, error)
	UpdateDirectoryRecord(record *model.DirectoryMonitorRecord) error
	DeleteDirectoryRecord(id string) error
	ListAllDirectoryRecords() ([]*model.DirectoryMonitorRecord, error)

	// 文件监控相关
	CreateFileRecord(record *model.FileMonitorRecord) error
	GetFileRecordByID(id string) (*model.FileMonitorRecord, error)
	GetFileRecordByPath(directoryID, filePath string) (*model.FileMonitorRecord, error)
	UpdateFileRecord(record *model.FileMonitorRecord) error
	DeleteFileRecord(id string) error
	DeleteFileRecordsByDirectoryID(directoryID string) error
	DeleteAllFileRecords() error
	GetFileRecordsByDirectoryID(directoryID string) ([]*model.FileMonitorRecord, error)
	GetAllIndexedFiles() ([]*model.FileMonitorRecord, error)

	// 文件变更相关
	CreateFileChange(change *model.FileMonitorChange) error
	GetFileChange(id string) (*model.FileMonitorChange, error)
	UpdateFileChange(change *model.FileMonitorChange) error
	DeleteFileChange(id string) error
	GetUnprocessedFileChanges() ([]*model.FileMonitorChange, error)
	GetFileChangesByDirectoryID(directoryID string) ([]*model.FileMonitorChange, error)

	// 目录哈希相关
	SaveDirectoryHashInfo(info *model.DirectoryHashInfo) error
	GetDirectoryHashInfo(directoryID string) (*model.DirectoryHashInfo, error)
	DeleteDirectoryHashInfo(directoryID string) error

	// 初始化数据库
	InitDB() error
	Close() error
}

// fileMonitorRepository 文件监控仓库实现
type fileMonitorRepository struct {
	db *sql.DB
}

// NewFileMonitorRepository 创建文件监控仓库实例
func NewFileMonitorRepository(dbPath string) (FileMonitorRepository, error) {
	db, err := sql.Open("sqlite", dbPath)
	if err != nil {
		return nil, fmt.Errorf("failed to open database: %w", err)
	}

	repo := &fileMonitorRepository{
		db: db,
	}

	// 初始化数据库表结构
	if err := repo.InitDB(); err != nil {
		db.Close()
		return nil, fmt.Errorf("failed to initialize database: %w", err)
	}

	return repo, nil
}

// InitDB 初始化数据库表结构
func (r *fileMonitorRepository) InitDB() error {
	// 创建目录监控表
	_, err := r.db.Exec(`
		CREATE TABLE IF NOT EXISTS directory_monitor (
			id TEXT PRIMARY KEY,
			directory_path TEXT UNIQUE NOT NULL,
			directory_name TEXT NOT NULL,
			collection_name TEXT NOT NULL,
			is_monitoring BOOLEAN DEFAULT FALSE,
			last_scan_time DATETIME,
			last_indexed_time DATETIME,
			total_files INTEGER DEFAULT 0,
			indexed_files INTEGER DEFAULT 0,
			created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
			updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
		)
	`)
	if err != nil {
		return fmt.Errorf("failed to create directory_monitor table: %w", err)
	}

	// 创建文件监控表
	_, err = r.db.Exec(`
		CREATE TABLE IF NOT EXISTS file_monitor (
			id TEXT PRIMARY KEY,
			directory_id TEXT NOT NULL,
			file_path TEXT NOT NULL,
			file_name TEXT NOT NULL,
			file_size INTEGER NOT NULL,
			file_mod_time DATETIME NOT NULL,
			file_hash TEXT NOT NULL,
			is_indexed BOOLEAN DEFAULT FALSE,
			indexed_at DATETIME,
			created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
			updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
			FOREIGN KEY (directory_id) REFERENCES directory_monitor(id) ON DELETE CASCADE,
			UNIQUE(directory_id, file_path)
		)
	`)
	if err != nil {
		return fmt.Errorf("failed to create file_monitor table: %w", err)
	}

	// 创建文件变更表
	_, err = r.db.Exec(`
		CREATE TABLE IF NOT EXISTS file_change (
			id TEXT PRIMARY KEY,
			directory_id TEXT NOT NULL,
			file_path TEXT NOT NULL,
			change_type TEXT NOT NULL,
			old_size INTEGER,
			new_size INTEGER,
			old_mod_time DATETIME,
			new_mod_time DATETIME,
			old_hash TEXT,
			new_hash TEXT,
			processed BOOLEAN DEFAULT FALSE,
			processed_at DATETIME,
			created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
			FOREIGN KEY (directory_id) REFERENCES directory_monitor(id) ON DELETE CASCADE
		)
	`)
	if err != nil {
		return fmt.Errorf("failed to create file_change table: %w", err)
	}

	// 创建目录哈希表
	_, err = r.db.Exec(`
		CREATE TABLE IF NOT EXISTS directory_hash (
			directory_id TEXT PRIMARY KEY,
			file_paths TEXT NOT NULL,
			version INTEGER DEFAULT 1,
			updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
			FOREIGN KEY (directory_id) REFERENCES directory_monitor(id) ON DELETE CASCADE
		)
	`)
	if err != nil {
		return fmt.Errorf("failed to create directory_hash table: %w", err)
	}

	// 创建索引
	_, err = r.db.Exec(`
		CREATE INDEX IF NOT EXISTS idx_file_monitor_directory_id ON file_monitor(directory_id);
		CREATE INDEX IF NOT EXISTS idx_file_monitor_file_path ON file_monitor(file_path);
		CREATE INDEX IF NOT EXISTS idx_file_change_directory_id ON file_change(directory_id);
		CREATE INDEX IF NOT EXISTS idx_file_change_processed ON file_change(processed);
	`)
	if err != nil {
		return fmt.Errorf("failed to create indexes: %w", err)
	}

	return nil
}

// GetAllIndexedFiles 获取所有已索引的文件
func (r *fileMonitorRepository) GetAllIndexedFiles() ([]*model.FileMonitorRecord, error) {
	query := `
		SELECT id, directory_id, file_path, file_name, file_size, file_mod_time, file_hash,
		       is_indexed, indexed_at, created_at, updated_at
		FROM file_monitor WHERE is_indexed = 1
		ORDER BY file_path
	`

	rows, err := r.db.Query(query)
	if err != nil {
		return nil, fmt.Errorf("failed to query indexed files: %w", err)
	}
	defer rows.Close()

	var files []*model.FileMonitorRecord
	for rows.Next() {
		var record model.FileMonitorRecord
		err := rows.Scan(
			&record.ID, &record.DirectoryID, &record.FilePath, &record.FileName, &record.FileSize,
			&record.FileModTime, &record.FileHash, &record.IsIndexed, &record.IndexedAt,
			&record.CreatedAt, &record.UpdatedAt,
		)
		if err != nil {
			return nil, fmt.Errorf("failed to scan file record: %w", err)
		}
		files = append(files, &record)
	}

	if err = rows.Err(); err != nil {
		return nil, fmt.Errorf("error iterating over file records: %w", err)
	}

	return files, nil
}

// Close 关闭数据库连接
func (r *fileMonitorRepository) Close() error {
	return r.db.Close()
}

// CreateDirectoryRecord 创建目录监控记录
func (r *fileMonitorRepository) CreateDirectoryRecord(record *model.DirectoryMonitorRecord) error {
	// 确保目录路径和目录名称不包含引号
	cleanPath := record.DirectoryPath
	if len(cleanPath) >= 2 && strings.HasPrefix(cleanPath, "\"") && strings.HasSuffix(cleanPath, "\"") {
		cleanPath = cleanPath[1 : len(cleanPath)-1]
	}
	
	cleanName := record.DirectoryName
	if len(cleanName) >= 2 && strings.HasPrefix(cleanName, "\"") && strings.HasSuffix(cleanName, "\"") {
		cleanName = cleanName[1 : len(cleanName)-1]
	}
	
	// 移除目录名称中可能存在的尾随逗号
	cleanName = strings.TrimSuffix(cleanName, ",")
	
	query := `
		INSERT INTO directory_monitor (
			id, directory_path, directory_name, collection_name, is_monitoring,
			last_scan_time, last_indexed_time, total_files, indexed_files, created_at, updated_at
		) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	`
	_, err := r.db.Exec(query,
		record.ID, cleanPath, cleanName, record.CollectionName, record.IsMonitoring,
		record.LastScanTime, record.LastIndexedTime, record.TotalFiles, record.IndexedFiles,
		record.CreatedAt, record.UpdatedAt)
	return err
}

// GetDirectoryRecordByID 根据ID获取目录监控记录
func (r *fileMonitorRepository) GetDirectoryRecordByID(id string) (*model.DirectoryMonitorRecord, error) {
	query := `
		SELECT id, directory_path, directory_name, collection_name, is_monitoring,
		       last_scan_time, last_indexed_time, total_files, indexed_files, created_at, updated_at
		FROM directory_monitor WHERE id = ?
	`
	row := r.db.QueryRow(query, id)

	var record model.DirectoryMonitorRecord
	err := row.Scan(
		&record.ID, &record.DirectoryPath, &record.DirectoryName, &record.CollectionName, &record.IsMonitoring,
		&record.LastScanTime, &record.LastIndexedTime, &record.TotalFiles, &record.IndexedFiles,
		&record.CreatedAt, &record.UpdatedAt)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, nil
		}
		return nil, err
	}
	return &record, nil
}

// GetDirectoryRecordByPath 根据路径获取目录监控记录
func (r *fileMonitorRepository) GetDirectoryRecordByPath(path string) (*model.DirectoryMonitorRecord, error) {
	query := `
		SELECT id, directory_path, directory_name, collection_name, is_monitoring,
		       last_scan_time, last_indexed_time, total_files, indexed_files, created_at, updated_at
		FROM directory_monitor WHERE directory_path = ?
	`
	row := r.db.QueryRow(query, path)

	var record model.DirectoryMonitorRecord
	err := row.Scan(
		&record.ID, &record.DirectoryPath, &record.DirectoryName, &record.CollectionName, &record.IsMonitoring,
		&record.LastScanTime, &record.LastIndexedTime, &record.TotalFiles, &record.IndexedFiles,
		&record.CreatedAt, &record.UpdatedAt)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, nil
		}
		return nil, err
	}
	return &record, nil
}

// UpdateDirectoryRecord 更新目录监控记录
func (r *fileMonitorRepository) UpdateDirectoryRecord(record *model.DirectoryMonitorRecord) error {
	// 确保目录路径和目录名称不包含引号
	cleanPath := record.DirectoryPath
	if len(cleanPath) >= 2 && strings.HasPrefix(cleanPath, "\"") && strings.HasSuffix(cleanPath, "\"") {
		cleanPath = cleanPath[1 : len(cleanPath)-1]
	}
	
	cleanName := record.DirectoryName
	if len(cleanName) >= 2 && strings.HasPrefix(cleanName, "\"") && strings.HasSuffix(cleanName, "\"") {
		cleanName = cleanName[1 : len(cleanName)-1]
	}
	
	// 移除目录名称中可能存在的尾随逗号
	cleanName = strings.TrimSuffix(cleanName, ",")
	
	query := `
		UPDATE directory_monitor SET
			directory_path = ?, directory_name = ?, collection_name = ?, is_monitoring = ?,
			last_scan_time = ?, last_indexed_time = ?, total_files = ?, indexed_files = ?, updated_at = ?
		WHERE id = ?
	`
	_, err := r.db.Exec(query,
		cleanPath, cleanName, record.CollectionName, record.IsMonitoring,
		record.LastScanTime, record.LastIndexedTime, record.TotalFiles, record.IndexedFiles,
		time.Now(), record.ID)
	return err
}

// DeleteDirectoryRecord 删除目录监控记录
func (r *fileMonitorRepository) DeleteDirectoryRecord(id string) error {
	_, err := r.db.Exec("DELETE FROM directory_monitor WHERE id = ?", id)
	return err
}

// ListAllDirectoryRecords 列出所有目录监控记录
func (r *fileMonitorRepository) ListAllDirectoryRecords() ([]*model.DirectoryMonitorRecord, error) {
	query := `
		SELECT id, directory_path, directory_name, collection_name, is_monitoring,
		       last_scan_time, last_indexed_time, total_files, indexed_files, created_at, updated_at
		FROM directory_monitor
	`
	rows, err := r.db.Query(query)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var records []*model.DirectoryMonitorRecord
	for rows.Next() {
		var record model.DirectoryMonitorRecord
		err := rows.Scan(
			&record.ID, &record.DirectoryPath, &record.DirectoryName, &record.CollectionName, &record.IsMonitoring,
			&record.LastScanTime, &record.LastIndexedTime, &record.TotalFiles, &record.IndexedFiles,
			&record.CreatedAt, &record.UpdatedAt)
		if err != nil {
			return nil, err
		}
		records = append(records, &record)
	}
	return records, nil
}

// CreateFileRecord 创建文件监控记录
func (r *fileMonitorRepository) CreateFileRecord(record *model.FileMonitorRecord) error {
	query := `
		INSERT INTO file_monitor (
			id, directory_id, file_path, file_name, file_size, file_mod_time, file_hash,
			is_indexed, indexed_at, created_at, updated_at
		) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	`
	_, err := r.db.Exec(query,
		record.ID, record.DirectoryID, record.FilePath, record.FileName, record.FileSize,
		record.FileModTime, record.FileHash, record.IsIndexed, record.IndexedAt,
		record.CreatedAt, record.UpdatedAt)
	return err
}

// GetFileRecordByID 根据ID获取文件监控记录
func (r *fileMonitorRepository) GetFileRecordByID(id string) (*model.FileMonitorRecord, error) {
	query := `
		SELECT id, directory_id, file_path, file_name, file_size, file_mod_time, file_hash,
		       is_indexed, indexed_at, created_at, updated_at
		FROM file_monitor WHERE id = ?
	`
	row := r.db.QueryRow(query, id)

	var record model.FileMonitorRecord
	err := row.Scan(
		&record.ID, &record.DirectoryID, &record.FilePath, &record.FileName, &record.FileSize,
		&record.FileModTime, &record.FileHash, &record.IsIndexed, &record.IndexedAt,
		&record.CreatedAt, &record.UpdatedAt)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, nil
		}
		return nil, err
	}
	return &record, nil
}

// GetFileRecordByPath 根据路径获取文件监控记录
func (r *fileMonitorRepository) GetFileRecordByPath(directoryID, filePath string) (*model.FileMonitorRecord, error) {
	query := `
		SELECT id, directory_id, file_path, file_name, file_size, file_mod_time, file_hash,
		       is_indexed, indexed_at, created_at, updated_at
		FROM file_monitor WHERE directory_id = ? AND file_path = ?
	`
	row := r.db.QueryRow(query, directoryID, filePath)

	var record model.FileMonitorRecord
	err := row.Scan(
		&record.ID, &record.DirectoryID, &record.FilePath, &record.FileName, &record.FileSize,
		&record.FileModTime, &record.FileHash, &record.IsIndexed, &record.IndexedAt,
		&record.CreatedAt, &record.UpdatedAt)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, nil
		}
		return nil, err
	}
	return &record, nil
}

// UpdateFileRecord 更新文件监控记录
func (r *fileMonitorRepository) UpdateFileRecord(record *model.FileMonitorRecord) error {
	query := `
		UPDATE file_monitor SET
			directory_id = ?, file_path = ?, file_name = ?, file_size = ?, file_mod_time = ?,
			file_hash = ?, is_indexed = ?, indexed_at = ?, updated_at = ?
		WHERE id = ?
	`
	_, err := r.db.Exec(query,
		record.DirectoryID, record.FilePath, record.FileName, record.FileSize,
		record.FileModTime, record.FileHash, record.IsIndexed, record.IndexedAt,
		time.Now(), record.ID)
	return err
}

// DeleteFileRecord 删除文件监控记录
func (r *fileMonitorRepository) DeleteFileRecord(id string) error {
	_, err := r.db.Exec("DELETE FROM file_monitor WHERE id = ?", id)
	return err
}

// DeleteFileRecordsByDirectoryID 根据目录ID删除文件监控记录
func (r *fileMonitorRepository) DeleteFileRecordsByDirectoryID(directoryID string) error {
	_, err := r.db.Exec("DELETE FROM file_monitor WHERE directory_id = ?", directoryID)
	return err
}

// DeleteAllFileRecords 删除所有文件监控记录
func (r *fileMonitorRepository) DeleteAllFileRecords() error {
	_, err := r.db.Exec("DELETE FROM file_monitor")
	return err
}

// GetFileRecordsByDirectoryID 根据目录ID获取文件监控记录
func (r *fileMonitorRepository) GetFileRecordsByDirectoryID(directoryID string) ([]*model.FileMonitorRecord, error) {
	query := `
		SELECT id, directory_id, file_path, file_name, file_size, file_mod_time, file_hash,
		       is_indexed, indexed_at, created_at, updated_at
		FROM file_monitor WHERE directory_id = ?
	`
	rows, err := r.db.Query(query, directoryID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var records []*model.FileMonitorRecord
	for rows.Next() {
		var record model.FileMonitorRecord
		err := rows.Scan(
			&record.ID, &record.DirectoryID, &record.FilePath, &record.FileName, &record.FileSize,
			&record.FileModTime, &record.FileHash, &record.IsIndexed, &record.IndexedAt,
			&record.CreatedAt, &record.UpdatedAt)
		if err != nil {
			return nil, err
		}
		records = append(records, &record)
	}
	return records, nil
}

// CreateFileChange 创建文件变更记录
func (r *fileMonitorRepository) CreateFileChange(change *model.FileMonitorChange) error {
	query := `
		INSERT INTO file_change (
			id, directory_id, file_path, change_type, old_size, new_size,
			old_mod_time, new_mod_time, old_hash, new_hash, processed, processed_at, created_at
		) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	`
	_, err := r.db.Exec(query,
		change.ID, change.DirectoryID, change.FilePath, change.ChangeType,
		change.OldSize, change.NewSize, change.OldModTime, change.NewModTime,
		change.OldHash, change.NewHash, change.Processed, change.ProcessedAt, change.CreatedAt)
	return err
}

// GetFileChange 获取文件变更记录
func (r *fileMonitorRepository) GetFileChange(id string) (*model.FileMonitorChange, error) {
	query := `
		SELECT id, directory_id, file_path, change_type, old_size, new_size,
		       old_mod_time, new_mod_time, old_hash, new_hash, processed, processed_at, created_at
		FROM file_change WHERE id = ?
	`
	row := r.db.QueryRow(query, id)

	var change model.FileMonitorChange
	err := row.Scan(
		&change.ID, &change.DirectoryID, &change.FilePath, &change.ChangeType,
		&change.OldSize, &change.NewSize, &change.OldModTime, &change.NewModTime,
		&change.OldHash, &change.NewHash, &change.Processed, &change.ProcessedAt, &change.CreatedAt)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, nil
		}
		return nil, err
	}
	return &change, nil
}

// UpdateFileChange 更新文件变更记录
func (r *fileMonitorRepository) UpdateFileChange(change *model.FileMonitorChange) error {
	query := `
		UPDATE file_change SET
			directory_id = ?, file_path = ?, change_type = ?, old_size = ?, new_size = ?,
			old_mod_time = ?, new_mod_time = ?, old_hash = ?, new_hash = ?,
			processed = ?, processed_at = ?
		WHERE id = ?
	`
	_, err := r.db.Exec(query,
		change.DirectoryID, change.FilePath, change.ChangeType,
		change.OldSize, change.NewSize, change.OldModTime, change.NewModTime,
		change.OldHash, change.NewHash, change.Processed, change.ProcessedAt, change.ID)
	return err
}

// DeleteFileChange 删除文件变更记录
func (r *fileMonitorRepository) DeleteFileChange(id string) error {
	_, err := r.db.Exec("DELETE FROM file_change WHERE id = ?", id)
	return err
}

// GetUnprocessedFileChanges 获取未处理的文件变更记录
func (r *fileMonitorRepository) GetUnprocessedFileChanges() ([]*model.FileMonitorChange, error) {
	query := `
		SELECT id, directory_id, file_path, change_type, old_size, new_size,
		       old_mod_time, new_mod_time, old_hash, new_hash, processed, processed_at, created_at
		FROM file_change WHERE processed = FALSE
		ORDER BY created_at ASC
	`
	rows, err := r.db.Query(query)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var changes []*model.FileMonitorChange
	for rows.Next() {
		var change model.FileMonitorChange
		err := rows.Scan(
			&change.ID, &change.DirectoryID, &change.FilePath, &change.ChangeType,
			&change.OldSize, &change.NewSize, &change.OldModTime, &change.NewModTime,
			&change.OldHash, &change.NewHash, &change.Processed, &change.ProcessedAt, &change.CreatedAt)
		if err != nil {
			return nil, err
		}
		changes = append(changes, &change)
	}
	return changes, nil
}

// GetFileChangesByDirectoryID 根据目录ID获取文件变更记录
func (r *fileMonitorRepository) GetFileChangesByDirectoryID(directoryID string) ([]*model.FileMonitorChange, error) {
	query := `
		SELECT id, directory_id, file_path, change_type, old_size, new_size,
		       old_mod_time, new_mod_time, old_hash, new_hash, processed, processed_at, created_at
		FROM file_change WHERE directory_id = ?
		ORDER BY created_at ASC
	`
	rows, err := r.db.Query(query, directoryID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var changes []*model.FileMonitorChange
	for rows.Next() {
		var change model.FileMonitorChange
		err := rows.Scan(
			&change.ID, &change.DirectoryID, &change.FilePath, &change.ChangeType,
			&change.OldSize, &change.NewSize, &change.OldModTime, &change.NewModTime,
			&change.OldHash, &change.NewHash, &change.Processed, &change.ProcessedAt, &change.CreatedAt)
		if err != nil {
			return nil, err
		}
		changes = append(changes, &change)
	}
	return changes, nil
}

// SaveDirectoryHashInfo 保存目录哈希信息
func (r *fileMonitorRepository) SaveDirectoryHashInfo(info *model.DirectoryHashInfo) error {
	// 将文件路径到哈希的映射转换为JSON字符串
	// 这里简化处理，实际应用中应该使用JSON序列化
	filePathsJSON := "{}" // 简化处理

	query := `
		INSERT OR REPLACE INTO directory_hash (directory_id, file_paths, version, updated_at)
		VALUES (?, ?, ?, ?)
	`
	_, err := r.db.Exec(query, info.DirectoryID, filePathsJSON, info.Version, time.Now())
	return err
}

// GetDirectoryHashInfo 获取目录哈希信息
func (r *fileMonitorRepository) GetDirectoryHashInfo(directoryID string) (*model.DirectoryHashInfo, error) {
	query := `
		SELECT directory_id, file_paths, version, updated_at
		FROM directory_hash WHERE directory_id = ?
	`
	row := r.db.QueryRow(query, directoryID)

	var info model.DirectoryHashInfo
	var filePathsJSON string
	err := row.Scan(&info.DirectoryID, &filePathsJSON, &info.Version, &info.UpdatedAt)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, nil
		}
		return nil, err
	}

	// 将JSON字符串解析为文件路径到哈希的映射
	// 这里简化处理，实际应用中应该使用JSON反序列化
	info.FilePaths = make(map[string]string)

	return &info, nil
}

// DeleteDirectoryHashInfo 删除目录哈希信息
func (r *fileMonitorRepository) DeleteDirectoryHashInfo(directoryID string) error {
	_, err := r.db.Exec("DELETE FROM directory_hash WHERE directory_id = ?", directoryID)
	return err
}
